using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Service.Goods.IGoodsService;
using JTZY.Admin.WebApi.Filters;
using JTZY.Service.Brand.IBrandService;
using JTZY.Model.Brand.Dto;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;

//创建时间：2024-11-09
namespace JTZY.Admin.WebApi.Controllers.Goods
{
    /// <summary>
    /// 代理货品池
    /// </summary>
    [Verify]
    [Route("Goods/Corecmsagentproducts")]
    public class CorecmsagentproductsController : BaseController
    {
        /// <summary>
        /// 代理货品池接口
        /// </summary>
        private readonly ICorecmsagentproductsService _CorecmsagentproductsService;
        private readonly ICorecmsbrandService _CorecmsbrandService;

        public CorecmsagentproductsController(ICorecmsagentproductsService CorecmsagentproductsService, ICorecmsbrandService CorecmsbrandService)
        {
            _CorecmsagentproductsService = CorecmsagentproductsService;
            _CorecmsbrandService = CorecmsbrandService;
        }

        /// <summary>
        /// 查询代理货品池列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:list")]
        public IActionResult QueryCorecmsagentproducts([FromQuery] CorecmsagentproductsQueryDto parm)
        {
            var response = _CorecmsagentproductsService.GetList(parm);
            var brandList = _CorecmsbrandService.GetAllList(new CorecmsbrandQueryDto());
            response.Result.ForEach(prod=>{

                prod.TotalStock = (prod.Stock.HasValue ? prod.Stock : 0) + (prod.MinStock.HasValue ? prod.MinStock : 0);
                var brand = brandList.Where(Brand => Brand.Id == prod.SkuBrandId).FirstOrDefault();
                prod.SkuBrandName = brand == null ? "" : brand.Name;
            });
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询代理货品池详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:query")]
        public IActionResult GetCorecmsagentproducts(long Id)
        {
            var response = _CorecmsagentproductsService.GetInfo(Id);
            
            var info = response.Adapt<CorecmsagentproductsDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加代理货品池
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:add")]
        [Log(Title = "代理货品池", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsagentproducts([FromBody] CorecmsagentproductsDto parm)
        {
            var modal = parm.Adapt<Corecmsagentproducts>().ToCreate(HttpContext);

            var response = _CorecmsagentproductsService.AddCorecmsagentproducts(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新代理货品池
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:edit")]
        [Log(Title = "代理货品池", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsagentproducts([FromBody] CorecmsagentproductsDto parm)
        {
            var modal = parm.Adapt<Corecmsagentproducts>().ToUpdate(HttpContext);
            var response = _CorecmsagentproductsService.UpdateCorecmsagentproducts(modal);

            return ToResponse(response);
        }

        [HttpPost("marketable")]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:edit")]
        [Log(Title = "代理货品池", BusinessType = BusinessType.UPDATE)]
        public IActionResult Marketable([FromBody] CorecmsagentproductsDto parm)
        {
            var modal = parm.Adapt<Corecmsagentproducts>().ToUpdate(HttpContext);
            modal.Marketable = !modal.Marketable;
            //上架需要刷新库存价格 暂未实现
            var response = _CorecmsagentproductsService.Marketable(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除代理货品池
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:delete")]
        [Log(Title = "代理货品池", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsagentproducts([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CorecmsagentproductsService.Delete(idArr));
        }

    }
}